<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function juris_add_page() {
    $temp = drupal_get_form('juris_edit_form');
    return $temp;
}

function juris_edit_page($id) {
    $temp = drupal_get_form('juris_edit_form', $id);
    return $temp;
}

function juris_edit_form($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = juris_load($id);
        if (empty($record)) {
            drupal_set_message(t('管辖区域不存在！'), 'status', FALSE);
            drupal_goto('sales/juris/list');
        }
    }

    $form['juris'] = array(
        '#type' => 'textfield',
//        '#title' => '管辖区域',
        '#size' => 32,
        '#maxlength' => 10,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? $record->juris_name:'',
    );

//    $salesmen = db_select('employee', 'E')
//        -> fields('E',array('employee_id', 'employee_name'))
//        -> condition('department_id',1, '=')
//        -> execute();
//    foreach ($salesmen as $salesman) {
//        $salesman_options[$salesman->employee_id] = $salesman->employee_name;
//    }
//
//    $form['salesman'] = array(
//        '#title' => '销售员名称',
//        '#type' => 'select',
//        '#options' => $salesman_options,
//        '#default_value' => !empty($record) ? $record->salesman_id : '',
//        '#required' => TRUE,
//    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => isset($record) ? '修改' : '添加',
        '#access' => juris_edit_form_submit_access($form_state, $id),
    );
    return $form;
}

function juris_edit_form_validate($form, &$form_state) {

}

function juris_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (empty($id)) { //add new juris
        $nid = db_insert('juris') // Table name no longer needs {}
        ->fields(array(
            'juris_name' => $form_state['values']['juris'],
//            'salesman_id' => $form_state['values']['salesman'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('添加成功！'), 'status', FALSE);
        }
//        drupal_goto('sales/juris/list');
    } else {
        $nid = db_update('juris') // edit exsting store. todo: update only modified fields
        ->fields(array(
            'juris_name' => $form_state['values']['juris'],
//            'salesman_id' => $form_state['values']['salesman'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            -> condition('juris_id',$id, '=')
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('修改成功！'), 'status', FALSE);
        }
//        drupal_goto('sales/juris/list');
    }
}

function juris_edit_form_submit_access(&$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;

    if ($id) {
        //edit
        $juris = juris_load($id);
        if ($juris) {
            $user_info = _get_user_info($user->uid);
            if ($juris->author_uid == $user->uid|| $user_info['role'] === 'administrator') {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            //load false
            drupal_set_message(t('管辖区域不存在！'), 'status', FALSE);
            return FALSE;
        }
    } else {
        //add
        return user_access('manage juris');
    }
}

function juris_load($id) {
    $result = db_select('juris', 'P')
        -> fields('P')
        -> condition('P.juris_id',$id, '=')
        -> execute()
        -> fetchObject();
    return $result;
}
